<?php
namespace app\controller\api;

use app\exception\ErrnoPlus;
use app\service\AliyunService;
use app\service\DeviceInfoTableService;
use app\service\UserDeviceBindingService;
use app\service\UserService;
use app\validate\AliyunValidate;
use app\validate\UserBaseUpdateValidate;
use share\controller\ApiBaseController;
use app\validate\LoginValidate;
use share\exception\ApiException;
use share\exception\Errno;
use share\service\ContextService;
use share\service\LicenseService;
use share\service\NotifyDingTalkService;
use share\service\UserBaseService;
use share\utils\Helper;

class AliyunController extends ApiBaseController{
      
    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new AliyunValidate();
    }

    public function account_device_action(){
        // TODO 接口貌似没用到 暂时不知道是干啥的        
        NotifyDingTalkService::pushTextByAMQP("通过account api 获取阿里云的设备信息.但该接口未实现".json_encode($this->request->param()));

        return self::ok([]);
        // $input = $this->check();
        // $account = $input['account'];
        // unset($input['account']);
        // $userInfo = UserService::getByAccount($account,$input);
        // if(empty($userInfo)){
        //     throw new ApiException("user not exists",ErrnoPlus::COMMON_ERROR);
        // }
        // if(empty($userInfo['server_site'])){
        //     throw new ApiException("server_site error",ErrnoPlus::COMMON_ERROR);
        // }
        // $input['serverSite'] = $userInfo['server_site'];
        // $input['identityId'] = $userInfo['identity_id'];
        // $deviceInfo = AliyunService::getDeviceBaseInfo($input);
        // $userIgnoreKeys = [
        //     "twitter_id",
        //     "wechat_id",
        //     "facebook_id",
        //     "microblog_id",
        //     "qq_id",
        //     "apple_id",
        //     "password",
        //     "token",
        // ];
        // $userRespInfo = [];
        // foreach($userInfo as $key => $val){
        //     if(in_array($key,$userIgnoreKeys)){
        //         continue;
        //     }
        //     if($key == 'sex'){
        //         $name = "性别";
        //         $msg = lang($key . "_" . $val);
        //     }
        //     else{
        //         $name = lang($key);
        //         $msg = $val;
        //     }
        //     $userRespInfo[] = [
        //         'name'=>$name,
        //         'msg'=>$msg,
        //     ];
        // }
        // $deviceList = [];
        // $deviceAllowKey = ['thingType','netType','owned'];
        // foreach ($deviceInfo as $device) {
        //     $current = [
        //         'nickName' => '',
        //     ];
        //     foreach($deviceAllowKey as $key){
        //         if($key == 'owned'){
        //             $current[$key] = lang($key . "_" . $device[$key]);
        //         }
        //         else{
        //             $current[$key] = lang($device[$key]);
        //         }
        //     }    
    
        //     $deviceList[] = $current;
        // }
        // return self::ok(["userInfo" => $userRespInfo, "deviceInfo" => $deviceList]);
    }

    /**
     * 获取设备物属性
     */
    public function thing_properties_action(){
        // TODO 接口貌似没用到 暂时不知道是干啥的        
        NotifyDingTalkService::pushTextByAMQP("通过account api 获取阿里云的物属性.但该接口未实现".json_encode($this->request->param()));

    }

    /**
     * 物属性格式化
     */
    protected function formatDataOfThingProperties($data){
        $res = [];
        foreach ($data as $vo) {
            $attr = $vo["attribute"];
            if($attr == "DefaultRouteConfig"){
                $tmp["attr"] = lang("DefaultRoute");
                $tmp["value"] = $vo["value"]["DefaultRoute"];
                $res[] = $tmp;
                $tmp["attr"] = lang("NetType");
                $tmp["value"] = lang("NetType_" . $vo["value"]["NetType"]);
                $res[] = $tmp;
            }
            else if($attr == 'NetLteConfig'){
                foreach ($vo["value"] as $key => $item) {
                    $tmp["attr"] = lang($key);
                    if ($key === "LinkStatus" || $key === "ErrorCode") {
                        $tmp["value"] = lang($key . "_" . $item);
                    } else {
                        $tmp["value"] = $item;
                    }
                    $res[] = $tmp;
                }
            }
            else if($attr == 'SmartAbility'){
                $tmp["attr"] = lang("SmartAbility");
                // 感觉原方法是错的  这里没改
                $tmp["value"] = implode('/',array_column(Helper::filter($vo['value'],['Name'=>"EBike"]),'info'));
                $res[] = $tmp;
            }
            else{
                $tmp = [
                    "attr" => lang($attr)
                ];
                if (is_string($vo["value"]) || $attr === "AlarmInCount" || $attr === "AlarmOutCount" || $attr === "ChanNum") {
                    $tmp["value"] = $vo["value"];
                    $res[] = $tmp;
                }
                if (is_numeric($vo["value"])) {
                    $tmp["value"] = lang($attr . "_" . $vo["value"]);
                    $res[] = $tmp;
                }
            }          
        }
        return $res;
    }
}
